//
//  HKIdentify.m
//  HKUtilities
//
//  Created by 胡锦涛 on 2018/10/31.
//  Copyright © 2018 胡锦涛. All rights reserved.
//

#import "HKIdentify.h"
#import "HKMacro.h"

NSString *const userTokenId = @"token";
NSString *const userPhone = @"phone";

@implementation HKIdentify
-(void)setValue:(id)value forKey:(NSString *)key
{
    if ([value isKindOfClass:[NSNumber class]]) {
        value = [NSString stringWithFormat:@"%@",value];
    }else if ([value isKindOfClass:[NSNull class]]){
        value = @"";
    }else if ([value isKindOfClass:[NSString class]]){
        if ([(NSString *)value containsString:@"<null>"]) {
            value = @"";
        }
        if ([(NSString *)value containsString:@"(null)"]) {
            value = @"";
        }
    }
    [super setValue:value forKey:key];
}

-(void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    
}
+ (HKIdentify *)sharedInstance
{
    static dispatch_once_t  onceToken;
    static HKIdentify * _sharedInstance;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [[self alloc] init];
    });
    return _sharedInstance;
}

-(id)init
{
    self = [super init];
    if (self) {
        
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *deviceId = [defaults stringForKey:@"deviceId"];
        if (!deviceId) {
            deviceId =  HK_MyUUID;
            [defaults setObject:deviceId forKey:@"deviceId"];
        }
        self.deviceId = deviceId;
        
        NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
        NSString *lang = nil;
        if (languages && [languages count] >= 1) {
            lang = [languages objectAtIndex:0];
            if ([lang isEqualToString:@"zh-Hans"]) {
                lang = @"zh_CN";
            } else if ([lang isEqualToString:@"zh-Hant"]) {
                lang = @"zh_TW";
            } else {
                lang = @"en__";
            }
        } else {
            lang = @"zh_CN";
        }
        self.language = lang;
        [defaults synchronize];
    }
    return self;
}

-(void)setIsAudit:(BOOL)isAudit {
    [HKUserDefaults setBool:isAudit forKey:@"isAudit"];
    [HKUserDefaults synchronize];
    _isAudit = isAudit;
}

-(void)setUserState:(BOOL)userState{
    [HKUserDefaults setBool:userState forKey:@"userState"];
    [HKUserDefaults synchronize];
    _userState = userState;
}

-(void)setOpenId:(NSString *)openId{
    [HKUserDefaults setObject:openId forKey:@"openId"];
    _openId = openId;
    [HKUserDefaults synchronize];
}

- (void)setPhoneNum:(NSString *)phoneNum {
    [HKUserDefaults setObject:phoneNum forKey:@"phoneNum"];
    _phoneNum = phoneNum;
    [HKUserDefaults synchronize];
}

- (NSString *)getToken{
    return [HKUserDefaults objectForKey:userTokenId];
}

- (void)setToken:(NSString *)token {
    _token = token;
    if (token.length) {
        [HKUserDefaults setObject:token forKey:userTokenId];
        [HKUserDefaults synchronize];
    }
}

- (void)setName:(NSString *)name {
    [HKUserDefaults setValue:name forKey:@"name"];
    [HKUserDefaults synchronize];
    _name = name;
}

- (void)logout {
    self.token = @"";
    self.loginType = @"";
    self.openId = @"";
    self.salt = @"";
    self.phoneNum = @"";
    self.name = @"我";
    self.userState = NO;
}
@end
